package dbcontrol

import (
	"MqttCloudProtocolServer/publiclib/datamodel/dborm"
	"errors"

	uuid "github.com/satori/go.uuid"
)

//UserserverControl struct
type UserserverControl struct {
	base           dbOperabase
	Data           *dborm.Userserver
	ParentUser     *UserconfControl
	ParentMqttConf *MqttconfControl
}

//CreateUserserverControl 初始化几种指针函数
func CreateUserserverControl() *UserserverControl {
	content := new(UserserverControl)
	content.base.GetData = func() interface{} {
		return content.Data
	}
	content.base.SetData = func(data interface{}) {
		if data != nil {
			content.Data = data.(*dborm.Userserver)
		}
	}
	content.base.GetKey = func() interface{} {
		if content.Data == nil {
			return nil
		}
		return content.Data.UUID
	}
	content.base.CreateNew = func() interface{} {
		res := new(dborm.Userserver)
		uuid := uuid.NewV4()
		res.UUID = uuid.String()
		return res
	}
	content.base.CreateNil = func() interface{} {
		return new(dborm.Userserver)
	}
	return content
}

//GetAll 获取全部用户-服务器映射信息
func (_this *UserserverControl) GetAll() ([]*dborm.Userserver, error) {
	res := make([]*dborm.Userserver, 0)
	_, err := _this.base.GetAllByPage(&res, 0, 0, 0)
	return res, err
}

//GetAllByPage 获取全部用户-服务器映射信息.totPage<=0时，计算分页页数,返回值第二个参数为计算或传入的页数
func (_this *UserserverControl) GetAllByPage(totPage, currentPage, countOfPage int) ([]*dborm.Userserver, int, error) {
	res := make([]*dborm.Userserver, 0)
	calTotpage, err := _this.base.GetAllByPage(&res, totPage, currentPage, countOfPage)
	return res, calTotpage, err
}

//CreateNew 创建一条记录
func (_this *UserserverControl) CreateNew() {
	_this.Data = _this.base.CreateNew().(*dborm.Userserver)
}

//FindByID 根据主键获取一条记录
func (_this *UserserverControl) FindByID(uuid string) error {
	return _this.base.FindByID(uuid)
}

//Save 更新当前条目
func (_this *UserserverControl) Save() error {
	return _this.base.Save()
}

//Delete 删除当前条目
func (_this *UserserverControl) Delete() error {
	return _this.base.Delete()
}

//GetByMQTTID fun
func (_this *UserserverControl) GetByMQTTID(tcpID string) ([]*dborm.Userserver, error) {
	res := make([]*dborm.Userserver, 0)
	err := _this.base.FindByWhere(&res, 0, 0, "mqtt_conf_id = ?", tcpID)
	return res, err
}

//GetByUserName fun
func (_this *UserserverControl) GetByUserName(name string) ([]*dborm.Userserver, error) {
	userControl := CreateUserconfControl()
	if userControl == nil {
		return nil, errors.New("数据库连接失败")
	}
	if err := userControl.FindByName(name); err != nil {
		return nil, errors.New("用户数据查找失败")
	}
	if userControl.Data == nil {
		return nil, errors.New("没有查到用户信息")
	}
	res := make([]*dborm.Userserver, 0)
	err := _this.base.FindByWhere(&res, 0, 0, "user_id = ?", userControl.Data.UUID)
	return res, err
}

//GetParentUser fun
func (_this *UserserverControl) GetParentUser() error {
	userconf := CreateUserconfControl() // new(UserconfControl)
	if err := userconf.FindByID(_this.Data.UserID); err != nil {
		return err
	}
	_this.ParentUser = userconf
	return nil
}

//GetParentMqttConf fun
func (_this *UserserverControl) GetParentMqttConf() error {
	mqttConf := CreateMqttconfControl() // new(MqttconfControl)
	if err := mqttConf.FindByID(_this.Data.MqttConfID); err != nil {
		return err
	}
	_this.ParentMqttConf = mqttConf
	return nil
}
